package com.lane.sidecn.security.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 权限控制
 * @author 忽吉烈
 *
 */
public class AuthorityServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8425833817712970097L;
	public static final int NO_LOGIN = 1;
	public static final int NO_AUTH = 2;
	public static final int LOGIN_SUCCESS = 3;
	public static final int LOGIN_FAIL = 4;
	public static final int LOGOUT = 5;

	/**
	 * Constructor of the object.
	 */
	public AuthorityServlet() {
		super();
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String type = request.getParameter("type");
		int intType = 0;
		try {
			intType = Integer.parseInt(type);
		} catch (Exception e) {
			intType = 0;
		}
		switch (intType) {
		case NO_LOGIN:
			noLogin(request, response);
			break;
		case NO_AUTH:
			noAuthority(request, response);
			break;
		case LOGIN_SUCCESS:
			loginSuccess(request, response);
			break;
		case LOGIN_FAIL:
			loginFail(request, response);
			break;
		case LOGOUT:
			logout(request, response);
			break;
		default:
			break;
		}
	}

	/**
	 * 未登录
	 * @throws IOException 
	 */
	private void noLogin(HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter out;
		try {
			out = response.getWriter();
			out.println("noLogin");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 没有权限访问资源
	 * @throws Exception
	 */
	private void noAuthority(HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter out;
		try {
			out = response.getWriter();
			out.println("noAuthority");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 登录成功
	 * @throws Exception
	 */
	private void loginSuccess(HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter out;
		try {
			out = response.getWriter();
			out.println("loginSuccess");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 登录失败
	 * @throws Exception
	 */
	private void loginFail(HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter out;
		try {
			out = response.getWriter();
			out.println("loginFail");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 退出
	 * @throws Exception
	 */
	private void logout(HttpServletRequest request, HttpServletResponse response) {
		PrintWriter out;
		try {
			out = response.getWriter();
			out.println("logout");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
